package com.cookor.poi.modules.sys.utils;


import java.util.Map;


import com.cookor.poi.common.utils.EhCacheUtils;
import net.sf.ehcache.Ehcache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class MobileUtils {
    protected static final Logger logger = LoggerFactory
            .getLogger(MobileUtils.class);
    private static ThreadLocal<Map<String, Object>> requestThreadLocalHolder = new ThreadLocal<Map<String, Object>>();

    private static class ThreadContextHolder {

        private static ThreadLocal<String> sessionIdThreadLocalHolder = new ThreadLocal<String>();

        public static void setSessionId(String sessionId) {
            sessionIdThreadLocalHolder.set(sessionId);
        }

        public static String getSessionId() {
            return sessionIdThreadLocalHolder.get();
        }
    }

    public static void setSessionValue(Object value) {
        String key = ThreadContextHolder.getSessionId();
        EhCacheUtils.put(key, value);
    }

    public static void setSessionId(String sessionId) {
        ThreadContextHolder.setSessionId(sessionId);
    }

    public static Map<String, Object> getSessionValue() {
        String key = ThreadContextHolder.getSessionId();
        Object object = EhCacheUtils.get(key);
        if(object==null){
            Map<String, Object> map =Maps.newConcurrentMap();
            return map;
        }else{
            return (Map<String, Object>)object;
        }
    }

    public static void setRequstValue(Map<String, Object> map) {
        requestThreadLocalHolder.set(map);
    }

    public static Map<String, Object> getRequestValue() {
        return requestThreadLocalHolder.get();
    }

    public static String getOpenid() {
        Map<String, Object> map = (Map<String, Object>) getSessionValue();
        return map.get("openid") == null ? null : map.get("openid").toString();
    }

    public static String getOpenid(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        Object obj = session.getAttribute("openId");
        if(obj!=null){
            return obj.toString();
        }
        return null;
        //return "111";
        //return "fLXtqBfZ/0EuyLSgzahNcwSX7bgGzuifMJHfpgmwE9SsNot2AHO+c/d42lI6X70F";
//		String pay_test = Global.getConfig("pay_test");
//		if("1".equals(pay_test)){
//			String mobile = getMobile();
//			String testMobile = Global.getConfig("test_mobile");
//			if(StringUtils.isNotBlank(mobile)&&testMobile.indexOf(mobile)>-1) {
//				return "fLXtqBfZ/0EuyLSgzahNcwSX7bgGzuifMJHfpgmwE9SsNot2AHO+c/d42lI6X70F";
//			}
//		}
//		Map<String, Object> map = (Map<String, Object>) getSessionValue();
//		return map.get("openid") == null ? null : map.get("openid").toString();
    }

    public static String getMobile(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        Object obj = session.getAttribute("mobile");
        if(obj!=null){
            return obj.toString();
        }
        return null;
        //Map<String, Object> map = (Map<String, Object>) getSessionValue();
        //return map.get("mobile") == null ? null : map.get("mobile").toString();
    }

    public static String getTime() {
        Map<String, Object> map = (Map<String, Object>) getRequestValue();
        return map.get("timestamp") == null ? null : map.get("timestamp")
                .toString();
    }

    public static String getClientip() {
        Map<String, Object> map = (Map<String, Object>) getRequestValue();
        return map.get("clientip") == null ? null : map.get("clientip")
                .toString();
    }



    public static String getActionId(){
        Map<String, Object> map = (Map<String, Object>) getRequestValue();
        return map.get("actionId") == null ? null : map.get("actionId")
                .toString();
    }

    public static String getSiteRoot() {
        Map<String, Object> map = (Map<String, Object>) getRequestValue();
        return map.get("siteroot") == null ? null : map.get("siteroot")
                .toString();
    }
    public static boolean getIsajax() {
        Map<String, Object> map = (Map<String, Object>) getRequestValue();
        return map.get("isajax") == null ? false : (Boolean)map.get("isajax");
    }

    public static String getSharer() {
        Map<String, Object> map = (Map<String, Object>) getRequestValue();
        return map.get("sharer") == null ? null : map.get("sharer")
                .toString();
    }


    public static Map<String,String> getShareData() {
        Map<String, Object> map = (Map<String, Object>) getRequestValue();
        return map.get("shareData") == null ? null : (Map<String,String>) map
                .get("shareData");
    }

}
